#!/usr/bin/perl -w
# CEPEL2PSAT converts CEPEL data file into PSAT data file
#
# CEPEL2PSAT <OPTIONS> FILEINPUT <FILEOUTPUT>
#
# Author:  Federico Milano
# Date:    09-Aug-2007
# Version: 1.0.0
#
# E-mail:  federico.milano@ucd.ie
#

use strict;

# -----------------------------------------------------------------------
# variable declaration
# -----------------------------------------------------------------------
my $nargin = 0;
my $verbose = 0;
my $helpmsg = 0;
my ($i,$h,$k);

my $ntitle = -1;
my $nbus = -1;
my $nsw = -1;
my $npv = -1;
my $npq = -1;
my $nsh = -1;
my $nline = -1;
my $narea = -1;
my $zone = 0;
my ($title1,@title,$ver);
my $pbas = 100;
my $freq = 60;
my $blanks = " " x 80;
my $format;

my @data;

my $value;

my $bustype;
my (@kvb,@kzo,@busidx,@busname,@busang,
    @busvol,@pgen,@pload,@qload,@qmi,@qma,@shur,@shux);
my (@swidx,@pvidx,@pqidx,@shidx);
my (@areanum,@areaexp,@areatol,@areaname);
my (@busfr,@bustt,@rest,@reat,@susc,@rateA,@rateS,@phs,@tap,@kt);
my %kvbase;

# -----------------------------------------------------------------------
# check inputs
# -----------------------------------------------------------------------
$nargin = @ARGV;
$nargin || die "Error: No input data file.\n";

# -----------------------------------------------------------------------
# check options
# -----------------------------------------------------------------------
while ($ARGV[0] =~ /^-/) {
    if ($ARGV[0] =~ /v/) {$verbose = 1;}
    if ($ARGV[0] =~ /h/) {$helpmsg = 1;}
    shift(@ARGV);
    $nargin--;
    if ($nargin == 0) {
	last;
    }
}

# -----------------------------------------------------------------------
# help (if requested)
# -----------------------------------------------------------------------
if ($helpmsg) {
    print "\nCEPEL2PSAT converts CEPEL data files into PSAT data files.\n\n";
    print "cepel2psat <options> fileinput <fileoutput>\n";
    print "  -v   verbose\n";
    print "  -h   print this help and exit\n\n";
    print "Author:   Federico Milano\n";
    print "Date:     09-Aug-2007\n";
    print "Version:  1.0.0\n\n";
    print "E-mail:   Federico.Milano\@uclm.es\n";
    die "\n";
}

# -----------------------------------------------------------------------
# define output file name (if necessary)
# -----------------------------------------------------------------------
if ($nargin == 1) {
    $ARGV[1] = $ARGV[0];
    $ARGV[1] =~ s/^d*_*/d_/;
    $ARGV[1] =~ s/[^\w\.]/_/g;
    $ARGV[1] =~ s/\..+$/.m/;
} elsif ($nargin == 0) {
    die "Error: Input file name is missing.\n";
}

# -----------------------------------------------------------------------
# open input data file
# -----------------------------------------------------------------------
print "Opening CEPEL data file \"$ARGV[0]\"...\n";
carret();
open(IN,$ARGV[0]) || die "cannot open $ARGV[0]: $!\n";

# read data
while (<IN>) {

    chomp;
    $_ = $_ . $blanks;

    if (/^TITU/) {

	$_ = <IN>;
	chomp;
	$ntitle++;
	$title[$ntitle] = $_;

    } elsif (/^DOPC/) {

	while (<IN>) {
	    last if /^9999/;
	    next if /^\(/;
	}

    } elsif (/^DBAR/) {

	while (<IN>) {
	    chomp;
	    last if /^9999/;
	    next if /^\(/;
	    $nbus++;
	    $busidx[$nbus] = assign($_,1,4,0);
	    $busname[$nbus] = substr($_,9,12);
	    $busname[$nbus] =~ s/\'/-/;
	    $kzo[$nbus] = assign($_,71,72,1);
	    $kvb[$nbus] = assign($_,73,76,1);
	    $kvbase{$busidx[$nbus]} = $kvb[$nbus];
	    $pload[$nbus] = assign($_,56,60,0)/$pbas;
	    $qload[$nbus] = assign($_,61,65,0)/$pbas;
	    $busvol[$nbus] = assign($_,23,26,$kvb[$nbus])/$kvb[$nbus];
	    $busang[$nbus] = 3.14159265358979*assign($_,27,30,0)/180;
	    $shur[$nbus] = 0;
	    $shux[$nbus] = assign($_,66,70,0)/$pbas/$busvol[$nbus]/$busvol[$nbus];
	    $pgen[$nbus] = assign($_,31,35,0)/$pbas;
	    $qmi[$nbus] = assign($_,41,45,0)/$pbas;
	    $qma[$nbus] = assign($_,46,50,0)/$pbas;
	    $bustype = assign($_,8,8,0);
	    if ($bustype == 2) {
		$nsw++;
		$swidx[$nsw] = $nbus;
	    } elsif ($bustype == 1) {
		$npv++;
		$pvidx[$npv] = $nbus;
	    }
	    if ($shur[$nbus] != 0 || $shux[$nbus] != 0) {
		$nsh++;
		$shidx[$nsh] = $nbus;
	    }
	    if ($pload[$nbus] != 0 || $qload[$nbus] != 0) {
		$npq++;
		$pqidx[$npq] = $nbus;
	    }
	}


    } elsif (/^DLIN/) {

	while (<IN>) {
	    chomp;
	    last if /^9999/;
	    next if /^\(/;
	    $nline++;
	    $busfr[$nline] = assign($_,1,4,0);
	    $bustt[$nline] = assign($_,9,12,0);
	    $rest[$nline] = assign($_,18,23,0)/100;
	    $reat[$nline] = assign($_,24,29,0)/100;
	    $susc[$nline] = assign($_,30,35,0)/$pbas;
	    $rateA[$nline] = assign($_,61,64,0)/$pbas;
	    $rateS[$nline] = assign($_,65,68,0)/$pbas;
	    $tap[$nline] = assign($_,36,40,0);
	    if ($tap[$nline] > 0) {
		$k = assign($_,56,60,0);
		if ($k == $busfr[$nline]) {
		  $tap[$nline] = 1/$tap[$nline];
		}
	    }
	    $phs[$nline] = assign($_,51,55,0);
	    if (substr($_,15,1) eq "T") {
		$kt[$nline] = $kvbase{$busfr[$nline]}/$kvbase{$bustt[$nline]};
	    } else {
		$kt[$nline] = 0;
	    }
	}


    } elsif (/^DARE/) {

	while (<IN>) {
	    chomp;
	    last if /^9999/;
	    next if /^\(/;
	    $narea++;
	    $areaname[$narea] = substr($_,19,36);
	    $areanum[$narea] = assign($_,1,4,0);
	    $areaexp[$narea] = assign($_,9,14,0)/$pbas;
	    $areatol[$narea] = 0;
	}


    } elsif (/^FIM/) {

	last;

    }

}

# -----------------------------------------------------------------------
# close data file
# -----------------------------------------------------------------------
close(IN) || die "cannot close $ARGV[0]: $!\n";

# -----------------------------------------------------------------------
# open output data file
# -----------------------------------------------------------------------
print "Writing PSAT file \"$ARGV[1]\"...\n";
carret();
open(OUT,">$ARGV[1]") || die "cannot open $ARGV[1]: $!\n";

# -----------------------------------------------------------------------
# write output data file
# -----------------------------------------------------------------------
print OUT "% File generated by PSAT from CEPEL data file.\n";
print OUT "% "."-" x 78 . "\n";
print OUT "% Author:   Federico Milano\n";
print OUT "% E-mail:   Federico.Milano\@uclm.es\n";
print OUT "% "."-" x 78 . "\n";
print OUT "% Generic ANAREDE data format "."$ARGV[0]"."\n";
for ($i = 0; $i <= $ntitle; $i++) { print OUT "% $title[$i]\n"; }
print OUT "\n";

# -----------------------------------------------------------------------
# write Bus.con
# -----------------------------------------------------------------------
if ($nbus >= 0) {
    $format = "%4d %6.2f %8.5f %8.5f %2d 1;\n";
    print OUT "Bus.con = [ ...\n";
    for ($i = 0; $i <= $nbus; $i++) {
	printf OUT $format,$busidx[$i],$kvb[$i],
	$busvol[$i],0,$kzo[$i];
	#$busvol[$i],$busang[$i],$kzo[$i];
    }
    print OUT "   ];\n\n";
}

# -----------------------------------------------------------------------
# write SW.con
# -----------------------------------------------------------------------
if ($nsw >= 0) {
    printf OUT "SW.con = [ ...\n";
    $format = "%4d $pbas %6.2f %8.5f %8.5f %8.5f %8.5f 1.1 0.9 %8.5f 1 1 1];\n\n";
    $h = $swidx[0];
    printf OUT $format,$busidx[$h],$kvb[$h],$busvol[$h],$busang[$h],
    $qma[$h],$qmi[$h],$pgen[$h];
}

# -----------------------------------------------------------------------
# write PV.con
# -----------------------------------------------------------------------
if ($npv >= 0) {
    printf OUT "PV.con = [ ...\n";
    $format = "%4d $pbas %6.2f %8.5f %8.5f %8.5f %8.5f 1.1 0.9 1 1;\n";
    for ($i = 0; $i <= $npv; $i++) {
	$h = $pvidx[$i];
	printf OUT $format,$busidx[$h],$kvb[$h],$pgen[$h],$busvol[$h],
	$qma[$h],$qmi[$h];
    }
    printf OUT "   ];\n\n";
}

# -----------------------------------------------------------------------
# write PQ.con
# -----------------------------------------------------------------------
if ($npq >= 0) {
    printf OUT "PQ.con = [ ...\n";
    $format = "%4d $pbas %6.2f %8.5f %8.5f 1.1 0.9 1 1;\n";
    for ($i = 0; $i <= $npq; $i++) {
	$h = $pqidx[$i];
	printf OUT $format,$busidx[$h],$kvb[$h],$pload[$h],$qload[$h];
    }
    printf OUT "   ];\n\n";
}

# -----------------------------------------------------------------------
# write Shunt.con
# -----------------------------------------------------------------------
if ($nsh >= 0) {
    printf OUT "Shunt.con = [ ...\n";
    $format = "%4d $pbas %6.2f 60 %8.5f %8.5f 1;\n";
    for ($i = 0; $i <= $nsh; $i++) {
	$h = $shidx[$i];
	printf OUT $format,$busidx[$h],$kvb[$h],$shur[$h],$shux[$h];
    }
    printf OUT "   ];\n\n";
}

# -----------------------------------------------------------------------
# write Line.con
# -----------------------------------------------------------------------
if ($nline >= 0) {
    print OUT "Line.con = [ ...\n";
    $format = "%4d %4d $pbas %6.2f 60 0 %8.4f " .
	"%8.5f " x 5 . "%8.3f 0 %8.3f 1;\n";
    for ($i = 0; $i <= $nline; $i++) {
	printf OUT $format,$busfr[$i],$bustt[$i],
	$kvbase{$busfr[$i]},$kt[$i],$rest[$i],$reat[$i],$susc[$i],
	$tap[$i],$phs[$i],$rateA[$i],$rateS[$i];
    }
    printf OUT "   ];\n\n";
}

# -----------------------------------------------------------------------
# write Area.con
# -----------------------------------------------------------------------
if ($narea >= 0) {
    printf OUT "Area.con = [ ...\n";
    $format = "%4d %4d $pbas %8.5f %8.5f;\n";
    for ($i = 0; $i <= $narea; $i++) {
	printf OUT $format,$areanum[$i],$busidx[$swidx[0]],
	$areaexp[$i],$areatol[$i];
    }
    printf OUT "   ];\n\n";
}

# -----------------------------------------------------------------------
# write bus names
# -----------------------------------------------------------------------
$nbus >= 0 && print OUT "Bus.names = { ...\n";
$h = ($nbus+1) % 5;
if ($h == 0) {$h = 5;}
if (($nbus+1) > 5) {
    for ($i = 0; $i <= $nbus-$h; $i+=5) {
	print OUT "  '$busname[$i]'; '$busname[$i+1]'; " .
	    "'$busname[$i+2]'; '$busname[$i+3]'; '$busname[$i+4]';\n";
    }
}
print OUT "  ";
for ($i = $nbus-$h+1; $i <= $nbus-1; $i++) {
    print OUT "'$busname[$i]'; ";
}
print OUT "'$busname[$nbus]'};\n\n";

# -----------------------------------------------------------------------
# close output data file
# -----------------------------------------------------------------------
close(OUT) || die "cannot close $ARGV[1]: $!\n";
print "Conversion completed.\n";

# -----------------------------------------------------------------------
# function for formatting data and/or assigning default values
# -----------------------------------------------------------------------
sub assign {
    my $param;
    $param = substr($_[0],$_[1]-1,$_[2]-$_[1]+1);
    $param =~ s/\s*//g;
    unless ($param) {$param = "0";}
    unless ($param =~ /[1-9]/) {$param = $_[3];}
    return $param;
}

# -----------------------------------------------------------------------
# function for writing a separator
# -----------------------------------------------------------------------
sub carret { $verbose && print "-" x 50 . "\n"; }
